<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Template{
	
	var $theme = "default";
	var $layout = "default";
	var $skin = "default";
	var $page = "index";
	
	var $data = array();
	
	var $ci;
	
	function Template(){
		$this->ci =& get_instance();
	}
	
	function render($page = NULL){
		if($page != NULL){
			$this->setPage($page);
		}
		
		$this->data['theme'] = $this->theme;
		$this->data['layout'] = $this->layout;
		$this->data['skin'] = $this->skin;
		$this->data['page'] = $this->page;
		
		$this->load("layouts/{$this->layout}", $this->data);
	}
	
	function setTheme($theme){
		$this->theme = $theme;
	}
	
	function setLayout($layout){
		$this->layout = $layout;	
	}
	
	function setSkin($skin){
		$this->skin = $skin;
	}
	
	function setPage($page){
		$this->page = $page;
	}
	
	// add a simple template variable
	function addData($name, $value, $add = FALSE) {
		if($add){
			$data = NULL;
			if(!isset($this->data[$name]) || $this->data[$name] == NULL){
				$data = array();
			} else{
				$data = $this->data[$name];
			}
			array_push($data, $value);
			
			$this->data[$name] = $data;
		} else{
	    	$this->data[$name] = $value;
		}
	}
	
	// add a big template variable by reference
	function addDataByRef($name, &$value) {
	    $this->data[$name] = $value;
	}
	
	// add a component to be included
	function addSlot($name, $path, $data=null) {
		//Relativo al path? $path = $this->_templatePath . $path
		$this->data['slots'][$name]=&$this->ci->load->view($this->_templatePath . $this->_template . $path, $data, true);
	    //$this->_data['slots'][$name]=&$this->ci->load->view($path, $data, true);
	    
	}
	
	// add a HTML meta tag
	function addMeta($name, $content) {
	    $this->data['meta'][$name]=$content;
	}
	
	
	function addStatic($path, $type = ""){
		//Comprobar si existe		
		if(file_exists(APPPATH."views/templates/".$this->theme."/".$this->skin."/static/".$path))
		{
			$this->data['static'][]=$path;
		}	
			
		else if(file_exists(APPPATH."views/templates/".$this->theme."/default/static/".$path))
		{
			$this->data['static'][]=$path;
		}
		
		else if(file_exists(APPPATH."views/templates/default/default/static/".$path))
		{
			$this->data['static'][]=$path;
		}
		
		else
		{
			// No existe
		}
	}
	
	function addCss($path){
		$this->addStatic("style/" . $path, "style");	
	}
	
	function addJs($path){
		$this->addStatic("javascript/" . $path, "javascript");	
	}
	
	
	
/*** NOTIFICATIONS ***/
	function addInformation($text){
		$this->addData("information", $text, TRUE);
	}
	
	function addAttention($text){
		$this->addData("attention", $text, TRUE);
	}
	
	function addError($text){
		$this->addData("error", $text, TRUE);
	}
	
	function addSuccess($text){
		$this->addData("success", $text, TRUE);
	}
	
	/** Add Flash **/
	function addFlashInformation($text){
		$this->addFlashData("information", $text);
	}
	
	function addFlashAttention($text){
		$this->addFlashData("attention", $text);
	}
	
	function addFlashError($text){
		$this->addFlashData("error", $text);
	}
	
	function addFlashSuccess($text){
		$this->addFlashData("success", $text);
	}
	
	function addFlashData($tipo, $message){
		$data = $this->ci->session->flashdata($tipo);
		if(!isset($data) || $data == NULL){
			$data = array();
		}
		array_push($data, $message);
		
		$this->ci->session->set_flashdata($tipo, $data);
	}
	
	/** 
	* Load a view
	*
	* Will be the name of the view you want to load without the .php extension.
	*
	* @access public
	* @param string
	*/
	function load($page, $data = NULL, $return = FALSE){		
		if(isset($_GET) && $_GET != null && isset($_GET['viewdata'])){
			echo "<pre><b>Page</b>: $page\n<b>Data</b>:";
			print_r($data);
			echo "</pre>";			
		} else{
		
			$output = NULL;
			
			if(file_exists(APPPATH."views/templates/".$this->theme."/".$this->skin."/".$page.EXT))
			{
				$output = $this->ci->load->view ( "templates/".$this->theme."/".$this->skin."/".$page, $data, $return);
			}
			
			else if(file_exists(APPPATH."views/templates/".$this->theme."/default/".$page.EXT))
			{
				$output = $this->ci->load->view ( "templates/".$this->theme."/default/".$page, $data, $return);
			}
			
			else if(file_exists(APPPATH."views/templates/default/default/".$page.EXT))
			{
				$output = $this->ci->load->view ( "templates/default/default/".$page, $data, $return);
			}
			
			else
			{
				show_error("Page ".$page." can not be found.");
			}
			
			if($output != NULL && $return){
				return $output;
			}
		}
	} // load
	
	function file_exists($page){
		$exists = FALSE;
		
		if(file_exists(APPPATH."views/templates/".$this->theme."/".$this->skin."/".$page.EXT)) {
			$exists = TRUE;
		}
		
		else if(file_exists(APPPATH."views/templates/".$this->theme."/default/".$page.EXT)) {
			$exists = TRUE;
		}
		
		else if(file_exists(APPPATH."views/templates/default/default/".$page.EXT)) {
			$exists = TRUE;
		}
		
		return $exists;
	}
	
	
	//load tiene el problema de que cachea todas las variables
	function load_clear($page, $data = NULL){
		if(isset($_GET) && $_GET != null && isset($_GET['viewdata'])){
			echo "<pre><b>Page</b>: $page\n<b>Data</b>:";
			print_r($data);
			echo "</pre>";			
		} else{
			
			extract($data);
			
			if(file_exists(APPPATH."views/templates/".$this->theme."/".$this->skin."/".$page.EXT))
			{
				include APPPATH."views/templates/".$this->theme."/".$this->skin."/".$page.EXT;
			}
			
			else if(file_exists(APPPATH."views/templates/".$this->theme."/default/".$page.EXT))
			{
				include APPPATH."views/templates/".$this->theme."/default/".$page.EXT;
			}
			
			else if(file_exists(APPPATH."views/templates/default/default/".$page.EXT))
			{
				include APPPATH."views/templates/default/default/".$page.EXT;
			}
			
			else
			{
				show_error("Page ".$page." can not be found.");
			}
			
		}
	} // load
	
}